import React from "react";
import {ActionType, ParamsType, ProCard, ProColumns, ProTable, RequestData} from "@ant-design/pro-components";
import {Button, message, Popconfirm, Space, TablePaginationConfig, Tooltip} from "antd";
import CustomerProductEdit from "@/pages/organization/customer-manager/components/CustomerProductEdit";
import {
  addCustomerLike,
  deleteCustomerLike,
  getProductByCustomer,
  updateCustomerLike
} from "@/pages/organization/customer-manager/api";
import {DeleteOutlined, DiffOutlined, EditOutlined} from "@ant-design/icons";
import {Access} from "@@/exports";
import {emptyList} from "@/services/base-request";
import Record from "typescript/lib/lib.es5";
import {GetComponentProps, GetRowKey} from "rc-table/lib/interface";
import {SortOrder} from "antd/es/table/interface";
import {SearchConfig} from "@ant-design/pro-table/es/components/Form/FormRender";
import {ToolBarProps} from "@ant-design/pro-table/es/components/ToolBar";
import {SizeType} from "antd/es/config-provider/SizeContext";
import SimpleActionForm from "@/components/SimpleActionTable/SimpleActionForm";
import {FormInstance} from "antd/lib";

interface ISimpleActionTable<T> {
  title?: string,
  bordered?: boolean,
  ghost?: boolean,
  extra?: React.ReactNode,
  columns: ProColumns<T>[],
  actionRef?: React.RefObject<ActionType>,
  rowKey: string | keyof Record<string, any> | GetRowKey<Record<string, any>>,
  request?: (params: (ParamsType & {
    pageSize?: number;
    current?: number;
    keyword?: string
  }), sort: Record<string, SortOrder>, filter: Record<string, (string | number)[] | null>) => Promise<Partial<RequestData<Record<string, any>>>>,
  search?: false | SearchConfig,
  toolBarRender?: ToolBarProps<Record<string, any>>["toolBarRender"] | false,
  pagination?: false | TablePaginationConfig,
  size?: SizeType,
  onRow?: GetComponentProps<Record<string, any>>
  rowSelected?: boolean;
  formItems?: React.ReactElement[];
  addTrigger?: React.ReactElement;
  formWidth?: number;
}

const SimpleActionTable: React.FC<ISimpleActionTable<any>> = <T extends unknown>({
                                                                                   title = "",
                                                                                   bordered = false,
                                                                                   ghost = false,
                                                                                   extra,
                                                                                   actionRef,
                                                                                   rowKey,
                                                                                   columns,
                                                                                   search = false,
                                                                                   request,
                                                                                   toolBarRender = false,
                                                                                   pagination = false,
                                                                                   size = 'small',
                                                                                   onRow,
                                                                                   rowSelected,
                                                                                   formItems,
                                                                                   addTrigger,
                                                                                   formWidth
                                                                                 }: ISimpleActionTable<T>) => {

  const formRef = React.createRef<FormInstance>()

  const cardExtra = () => {
    return <Space>
      <SimpleActionForm
        formWidth={formWidth}
        title={`新增${title}`}
        onFinish={
          async (values: Record<string, any>) => {
            console.log(values, "values")
            return false
          }
        }
        formRef={formRef}
        trigger={
          addTrigger ? addTrigger : <Button type={"text"} icon={<DiffOutlined/>}/>
        }>
        {formItems}
      </SimpleActionForm>
    </Space>
  }
  return (
    <ProCard
      ghost={ghost}
      bordered={bordered}
      bodyStyle={{width: '100%'}}
      extra={extra ?? cardExtra()}
      title={title || ''}>
      <ProTable<T, API.PageParams>
        actionRef={actionRef}
        rowKey={rowKey}
        ghost
        rowClassName={(record: T) => {
          return rowSelected ? 'highlight-row' : '';
        }}
        onRow={onRow}
        toolBarRender={toolBarRender}
        search={search}
        pagination={pagination}
        request={request}
        size={size}
        columns={columns as any}
      />
    </ProCard>
  )
}

export default SimpleActionTable
